﻿using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rvt2ifcTool.Common
{
	internal class XeokitUtils
	{
		public static string BaseDirectory = "";
		public static string GetProjectDirectory(string basePath, string projectUuid)
		{
			string gProjectPath = Path.Combine(basePath, "projects");
			gProjectPath = Path.Combine(gProjectPath, projectUuid);
			// 检查并创建目标目录
			if (!Directory.Exists(gProjectPath))
			{
				Directory.CreateDirectory(gProjectPath);
			}
			return gProjectPath;
		}
		public static string GetProjectJsonPath(string projectPath)
		{

			return Path.Combine(projectPath, "index.json");
		}
		public static string GetModelDirectory(string projectPath, string modelUuid)
		{
			string path = Path.Combine(projectPath, "models", modelUuid); ;
			// 检查并创建目标目录
			if (!Directory.Exists(path))
			{
				Directory.CreateDirectory(path);
			}
			return path;
		}
		public static void CopyDirectory(string sourceDir, string destinationDir)
		{
			if (!Directory.Exists(destinationDir))
			{
				Directory.CreateDirectory(destinationDir);
			}

			foreach (string file in Directory.GetFiles(sourceDir))
			{
				string fileName = Path.GetFileName(file);
				string destFile = Path.Combine(destinationDir, fileName);
				File.Copy(file, destFile, true);
			}

			foreach (string subDir in Directory.GetDirectories(sourceDir))
			{
				string subDirName = Path.GetFileName(subDir);
				string destSubDir = Path.Combine(destinationDir, subDirName);
				CopyDirectory(subDir, destSubDir);
			}
		}

		public static void CreateLonelyIndexJsonFile(string indexJsonFilePath, string projectUuid, string modelUuid, string modelName)
		{
			Xeokit xeokit = new Xeokit();
			xeokit.id = projectUuid;
			xeokit.name = modelName;
			Model model = new Model();
			model.id = modelUuid;
			model.name = modelName;
			xeokit.models.Add(model);
			xeokit.viewerContent.modelsLoaded.Add(modelUuid);
			string json = JsonConvert.SerializeObject(xeokit, Formatting.Indented);
			File.WriteAllText(indexJsonFilePath, json);
		}
		public static string CreateIndexJsonFile(string indexJsonFilePath, string projectUuid, string projectName, List<string> modelUuids, List<string> modelNames)
		{
			Xeokit xeokit = new Xeokit();
			xeokit.id = projectUuid;
			xeokit.name = projectName;
			for (int i = 0; i < modelUuids.Count; i++)
			{
				string modelUuid = modelUuids[i];
				string modelName = modelNames[i];
				Model model = new Model();
				model.id = modelUuid;
				model.name = modelName;
				xeokit.models.Add(model);
				xeokit.viewerContent.modelsLoaded.Add(modelUuid);
			}
			string json = JsonConvert.SerializeObject(xeokit, Formatting.Indented);
			File.WriteAllText(indexJsonFilePath, json);
			return json;
		}
		public static void ExportXktAndJson(string inputIfc,
			string outputGlb,
			string outputJson,
			string outputXkt,
			string batFilePath)
		{
			string arguments = $"{inputIfc} {outputGlb} {outputJson} {outputXkt}";


			ProcessStartInfo processStartInfo = new ProcessStartInfo
			{
				FileName = batFilePath,
				Arguments = arguments,
				RedirectStandardOutput = true,
				RedirectStandardError = true,
				UseShellExecute = false,
				CreateNoWindow = true
			};

			using (Process process = new Process { StartInfo = processStartInfo })
			{
				process.Start();
				string output = process.StandardOutput.ReadToEnd();
				string error = process.StandardError.ReadToEnd();
				//MessageBox.Show(output);
				//MessageBox.Show(error);
				process.WaitForExit();

				if (!string.IsNullOrEmpty(error))
				{
					//MessageBox.Show(error);
				}

				if (process.ExitCode != 0)
				{
					//MessageBox.Show(error);
				}
			}


		}
		public static void ExportXkt(string ifcPath,
			string projectDirectory,
			string gScriptName,
			string mOutputGuid,
			string mModelUuid,
			string mModelName,
			string mProjectUuid
			)
		{
			string batFilePath = gScriptName;
			string inputIfc = ifcPath;
			string outputGlb = Path.Combine(Path.GetTempPath(), "xeokit", mOutputGuid + ".glb");
			string modelDirectory = GetModelDirectory(projectDirectory, mModelUuid);
			string indexJsonFilePath = GetProjectJsonPath(projectDirectory);
			CreateLonelyIndexJsonFile(indexJsonFilePath, mProjectUuid, mModelUuid, mModelName);

			string outputJson = Path.Combine(modelDirectory, "metadata.json");
			string outputXkt = Path.Combine(modelDirectory, "geometry.xkt");

			string arguments = $"{inputIfc} {outputGlb} {outputJson} {outputXkt}";

			try
			{
				ExportXktAndJson(inputIfc, outputGlb, outputJson, outputXkt, batFilePath);
			}
			catch (Exception ex)
			{
				//MessageBox.Show(ex.ToString());
			}
		}
	}
}
